﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GeneralDashboard.Controllers
{
    /// <summary>
    /// LineChart.xaml 的交互逻辑
    /// </summary>
    public partial class LineChart : UserControl
    {
        public LineChart()
        {
            InitializeComponent();
            webView.Source = new Uri(new FileInfo("Line.html").FullName);
            InitializeAsync();
        }
        public SolidColorBrush SerialColor
        {
            get { return (SolidColorBrush)GetValue(SerialColorProperty); }
            set { SetValue(SerialColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SerialColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SerialColorProperty =
            DependencyProperty.Register("SerialColor", typeof(SolidColorBrush), typeof(LineChart), new PropertyMetadata(new SolidColorBrush(Colors.BlueViolet), async (s, e) =>
            {
                var c = s as LineChart;
                var value = e.NewValue as SolidColorBrush;
                var color = ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(value.Color.A, value.Color.R, value.Color.G, value.Color.B));
                await c.webView.EnsureCoreWebView2Async();
                await c.webView.CoreWebView2.ExecuteScriptAsync($"setColor('{color}');");
            }));
        public async void InitializeAsync()
        {
            await webView.EnsureCoreWebView2Async();
            await webView.CoreWebView2.ExecuteScriptAsync(
                "window.addEventListener('dragover',function(e){e.preventDefault();},false);");
            await webView.CoreWebView2.ExecuteScriptAsync(
                "window.addEventListener('drop',function(e){" +
                "e.preventDefault();" +
                "console.log(e.dataTransfer);" +
                "console.log(e.dataTransfer.files[0])" +
                "}, false);");
            await webView.CoreWebView2.ExecuteScriptAsync(
                "window.addEventListener('contextmenu', window => {window.preventDefault();});");
        }

        private void testButton_Click(object sender, RoutedEventArgs e)
        {
            var data = (new Random().NextDouble() + 0.5) * 10;
            webView.CoreWebView2.ExecuteScriptAsync(
                $"pushData({data});");
        }
    }
}
